package cn.iocoder.yudao.module.measure.service.point.impl;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.measure.controller.admin.point.VO.DtuInfoPageReqVO;
import cn.iocoder.yudao.module.measure.controller.admin.point.VO.DtuInfoSaveReqVO;
import cn.iocoder.yudao.module.measure.dal.dataobject.point.DtuInfoDO;
import cn.iocoder.yudao.module.measure.dal.mysql.point.BusinessDtuInfoMapper;
import cn.iocoder.yudao.module.measure.service.point.BusinessDtuInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.measure.enums.ErrorCodeContants.DTU_INFO_NOT_EXISTS;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * DTU信息表
 * </p>
 *
 * @author zhengbzh
 * @date 2025-06-09 16:47:17
 * @create [2025-06-09 16:47:17] [zhengbzh] [代码生成器生成]
 */
@Slf4j
@Transactional(readOnly = true)
@Service
@Validated
public class BusinessDtuInfoServiceImpl implements BusinessDtuInfoService {

    @Resource
    private BusinessDtuInfoMapper dtuInfoMapper;

    @Override
    @Transactional
    public Long createDtuInfo(DtuInfoSaveReqVO createReqVO) {
        // 插入
        DtuInfoDO dtuInfo = BeanUtils.toBean(createReqVO, DtuInfoDO.class);
        dtuInfoMapper.insert(dtuInfo);

        // 返回
        return dtuInfo.getId();
    }

    @Override
    @Transactional
    public void updateDtuInfo(DtuInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateDtuInfoExists(updateReqVO.getId());
        // 更新
        DtuInfoDO updateObj = BeanUtils.toBean(updateReqVO, DtuInfoDO.class);
        dtuInfoMapper.updateById(updateObj);
    }

    @Override
    @Transactional
    public void deleteDtuInfo(Long id) {
        // 校验存在
        validateDtuInfoExists(id);
        // 删除
        dtuInfoMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void deleteDtuInfoListByIds(List<Long> ids) {
        // 删除
        dtuInfoMapper.deleteByIds(ids);
    }


    private void validateDtuInfoExists(Long id) {
        if (dtuInfoMapper.selectById(id) == null) {
            throw exception(DTU_INFO_NOT_EXISTS);
        }
    }

    @Override
    public DtuInfoDO getDtuInfo(Long id) {
        return dtuInfoMapper.selectById(id);
    }

    @Override
    public PageResult<DtuInfoDO> getDtuInfoPage(DtuInfoPageReqVO pageReqVO) {
        return dtuInfoMapper.selectPage(pageReqVO);
    }



    @Override
    public List<DtuInfoDO> findTree(String measurePointName) {
        return dtuInfoMapper.findTree(measurePointName);
    }








}


